/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import connectDB.ConnectDB;
import DAO.CodeName;
import DAO.NameToCode;
import java.awt.Color;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class frm_STAFF_A03 extends javax.swing.JFrame {

    ConnectDB conn3;
    ArrayList<String> arrList;
    String EmpCode;
    String ComName;
    private Date date;
    private Vector<String> v;
    private Vector<String> vectorUpdate;
    private String workClass;
    private CodeName getCodeName;

    public frm_STAFF_A03(String EmpCode, ConnectDB conn2, Date date) {
        initComponents();
        setLocation(400, 200);
        setResizable(false);
        this.conn3 = conn2;
        conn3.openConnection();
        this.EmpCode = EmpCode;
        getCodeName = new CodeName();
        v = new Vector<String>();
        vectorUpdate = new Vector<String>();
        this.date = date;

        setMode();    // Set mode = "Update" hay "Register" theo date user chọn từ A02
        checkMode();    // Kiểm tra Mode để thực hiện Register hay Update
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lbTitle = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        lbComName = new javax.swing.JLabel();
        lbEmpName = new javax.swing.JLabel();
        lbEmpClass = new javax.swing.JLabel();
        lbDate = new javax.swing.JLabel();
        cbWorkingClass = new javax.swing.JComboBox();
        cbStartTimeh = new javax.swing.JComboBox();
        cbStartTimem = new javax.swing.JComboBox();
        cbStartClass = new javax.swing.JComboBox();
        cbEndTimeh = new javax.swing.JComboBox();
        cbEndTimem = new javax.swing.JComboBox();
        cbEndClass = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        taNote = new javax.swing.JTextArea();
        btBack = new javax.swing.JButton();
        btRegister = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        lbTitle.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        lbTitle.setText("REGISTER");

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Company Name");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Employee Name");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Employee Class");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel5.setText("Date");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setText("Working Class");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel7.setText("Start Time");

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel8.setText("End Time");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel9.setText("Start Class");

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel10.setText("End Class");

        jLabel11.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel11.setText("Note");

        lbComName.setText("XXXXXXXXXXX");

        lbEmpName.setText("XXXXXXXXXXX");

        lbEmpClass.setText("XXXXXXXXXXXXX");

        lbDate.setText("yyyy/MM/dd");

        cbWorkingClass.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "XXXXXXXXXX" }));
        cbWorkingClass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbWorkingClassActionPerformed(evt);
            }
        });

        cbStartClass.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "XXXXXXXXXX" }));

        cbEndTimeh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "hh" }));

        cbEndTimem.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "mm" }));

        cbEndClass.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "XXXXXXXXXX" }));

        taNote.setColumns(20);
        taNote.setRows(5);
        jScrollPane1.setViewportView(taNote);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel11)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7))
                        .addGap(35, 35, 35)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(cbStartTimeh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbStartTimem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(cbEndTimeh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbEndTimem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(lbComName)
                            .addComponent(lbEmpName)
                            .addComponent(lbDate)
                            .addComponent(cbWorkingClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(58, 58, 58)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addGap(36, 36, 36)
                                .addComponent(cbStartClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lbEmpClass))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addGap(45, 45, 45)
                                .addComponent(cbEndClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(22, Short.MAX_VALUE))
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cbEndClass, cbStartClass, lbEmpClass});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(lbComName, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lbEmpName)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(lbEmpClass))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lbDate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cbWorkingClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel7)
                    .addComponent(cbStartTimeh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbStartTimem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(cbStartClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel10)
                    .addComponent(cbEndTimem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbEndTimeh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(cbEndClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        btBack.setText("BACK");
        btBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btBackActionPerformed(evt);
            }
        });

        btRegister.setText("REGISTER");
        btRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRegisterActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btBack)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btRegister))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(224, 224, 224)
                        .addComponent(lbTitle)))
                .addContainerGap(25, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(lbTitle)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btRegister)
                    .addComponent(btBack))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void setMode() {
        Calendar cal = Calendar.getInstance();
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");

        String daySelected = dateFormat.format(date);
        ResultSet rs = null;
        String query = "Select * From WORKTIME WRK Where WRK.EMP_CODE = '" + EmpCode + "'";
        try {
            rs = conn3.excuteQuery(query);
            while (rs.next()) {
                if (daySelected.equals(rs.getString("CAL_YMD").trim())) {
                    lbTitle.setText("UPDATE");
                    btRegister.setText("UPDATE");
                }
            }
        } catch (SQLException ex) {
            System.out.println("Wrong in ResultSet");
        }
    }

    private void checkMode() {
        excuteQuery();
        loadComName();
        loadEmpName();
        loadEmpClass();
        loadDate();
        if (lbTitle.getText().equalsIgnoreCase("UPDATE")) {
            excuteQueryToUpdate();
            loadWorkClassUpdate();
            loadStartClassUpdate();
            loadEndClassUpdate();
            //loadStartTimeUpdate();
            //loadEndTimeUpdate();
            loadNoteUpdate();

        } else {

            loadWorkClass();
            loadStartTime();
            loadEndTime();
            loadStartClass();
            loadEndClass();
        }
    }
    private void cbWorkingClassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbWorkingClassActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cbWorkingClassActionPerformed

    private void btBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btBackActionPerformed
        // Quay lại frm 2 khi nhấp
        frm_STAFF_A02 frm2 = new frm_STAFF_A02(conn3, EmpCode, date);
        frm2.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btBackActionPerformed

    private void btRegisterActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRegisterActionPerformed
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String daySelect = dateFormat.format(date); // Ngày lấy từ A04

        String WorkClass = cbWorkingClass.getSelectedItem().toString();
        String StartClass = cbStartClass.getSelectedItem().toString();
        String EndClass = cbEndClass.getSelectedItem().toString();
        String StartTimehh = cbStartTimeh.getSelectedItem().toString();
        String StartTimemm = cbStartTimem.getSelectedItem().toString();
        String EndTimehh = cbEndTimeh.getSelectedItem().toString();
        String EndTimemm = cbEndTimem.getSelectedItem().toString();
        String Note = taNote.getText().toString();

        NameToCode toCode = new NameToCode();
        try {
            String queryUpdate = "UPDATE WORKTIME ";
            queryUpdate += "SET ";
            queryUpdate += "WRK_KBN = '" + toCode.CodeWorkClass(WorkClass) + "' ,";
            queryUpdate += "WRK_GO_KBN =  '" + toCode.CodeStartClass(StartClass) + "' ,";
            queryUpdate += "WRK_LEFT_KBN = '" + toCode.CodeEndClass(EndClass) + "' ,";
            queryUpdate += "WRK_START_TIME = '" + (StartTimehh + StartTimemm) + "' ,";
            queryUpdate += "WRK_END_TIME = '" + (EndTimehh + EndTimemm) + "' ,";
            queryUpdate += "WRK_NOTE = '" + Note + "' ";
            queryUpdate += "WHERE ";
            queryUpdate += "EMP_CODE = '" + EmpCode + "' ";
            queryUpdate += "AND ";
            queryUpdate += "CAL_YMD = '" + daySelect + "' ";

            int rs = conn3.executeUpdate(queryUpdate);
            if (rs != -1) {
                JOptionPane.showMessageDialog(null, "Update successfull!");
                frm_STAFF_A04 frm4 = new frm_STAFF_A04(conn3, EmpCode, date);
                frm4.setVisible(true);
                setVisible(false);
            }


        } catch (Exception e) {
            System.out.println("Wrong in query Update A03");
        }

    }//GEN-LAST:event_btRegisterActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btBack;
    private javax.swing.JButton btRegister;
    private javax.swing.JComboBox cbEndClass;
    private javax.swing.JComboBox cbEndTimeh;
    private javax.swing.JComboBox cbEndTimem;
    private javax.swing.JComboBox cbStartClass;
    private javax.swing.JComboBox cbStartTimeh;
    private javax.swing.JComboBox cbStartTimem;
    private javax.swing.JComboBox cbWorkingClass;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbComName;
    private javax.swing.JLabel lbDate;
    private javax.swing.JLabel lbEmpClass;
    private javax.swing.JLabel lbEmpName;
    private javax.swing.JLabel lbTitle;
    private javax.swing.JTextArea taNote;
    // End of variables declaration//GEN-END:variables

    private void loadStartTime() {
        Calendar cal = new GregorianCalendar();
        cal.getTime();
        int hourNow = cal.get(Calendar.HOUR_OF_DAY);
        int minuteNow = cal.get(Calendar.MINUTE);

        cbStartTimeh.addItem("");
        cbStartTimem.addItem("");

        // StartTime : Hour
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                cbStartTimeh.addItem("0" + i);
            } else {
                cbStartTimeh.addItem(i);
            }

        }

        // StartTime : Minute
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                cbStartTimem.addItem("0" + i);
            } else {
                cbStartTimem.addItem(i);
            }
        }

        if (!cbWorkingClass.getSelectedItem().toString().equals("Normal")) {
            cbStartTimeh.setSelectedIndex(0);
            cbStartTimem.setSelectedIndex(0);

        } else {
            cbStartTimeh.setSelectedIndex((hourNow + 1));
            cbStartTimem.setSelectedIndex((minuteNow + 1));
        }
    }

    private void loadEndTime() {
        Calendar cal = new GregorianCalendar();
        cal.getTime();

        int hourNow = cal.get(Calendar.HOUR_OF_DAY);
        int minuteNow = cal.get(Calendar.MINUTE);

        cbEndTimeh.removeAllItems();
        cbEndTimem.removeAllItems();
        cbEndTimeh.addItem("");
        cbEndTimem.addItem("");


        // EndTime : Hour
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                cbEndTimeh.addItem("0" + i);
            } else {
                cbEndTimeh.addItem(i);
            }

        }

        // EndTime : Minute
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                cbEndTimem.addItem("0" + i);
            } else {
                cbEndTimem.addItem(i);
            }
        }
    }

    private void loadStartClass() {
        Calendar cal = new GregorianCalendar();
        cal.getTime();

        int hourNow = cal.get(Calendar.HOUR_OF_DAY);
        int minuteNow = cal.get(Calendar.MINUTE);

        cbStartClass.removeAllItems();
        cbStartClass.addItem("No Setting");
        cbStartClass.addItem("Normal");
        cbStartClass.addItem("Late");
        cbStartClass.addItem("Morning Absent");
        cbStartClass.addItem("Direct");

        if (cbWorkingClass.getSelectedItem().toString().equals("Normal")) {
            if (hourNow <= 8) { // Trước 8h hoặc 8h
                if (minuteNow > 0) {    // Lớn hơn 8h0 phút
                    cbStartClass.setSelectedIndex(2);   //Late
                } else {
                    cbStartClass.setSelectedIndex(1);   //Normal
                }
            } else {
                cbStartClass.setSelectedIndex(2);   //Late
            }
        } else {
            cbStartClass.setSelectedIndex(0);
        }
    }

    private void loadEndClass() {
        cbEndClass.removeAllItems();
        cbEndClass.addItem("No Setting");
        cbEndClass.addItem("Normal");
        cbEndClass.addItem("Early");
        cbEndClass.addItem("Afternoon Absent");
        cbEndClass.addItem("Direct");

        cbEndClass.setSelectedIndex(0);
    }

    private void loadDataA03() {
    }

    private void loadComName() {
        String ComName = v.get(0).toString();
        lbComName.setText(ComName);

        if (v.get(1).equals("1")) {
            lbComName.setForeground(Color.red);
        }
    }

    private void excuteQuery() {
        ResultSet rs = null;
        String query = "SELECT * FROM COMPANY_MST COM, EMPLOYEE_MST EMP "
                + "WHERE COM.COM_CODE = EMP.COM_CODE AND EMP.EMP_CODE = '" + EmpCode + "'";
        try {
            rs = conn3.excuteQuery(query);
            while (rs.next()) {
                v.add(rs.getString("COM_NAME").trim());
                v.add(rs.getString("COM_FLAG_DELETE").trim());
                v.add(rs.getString("EMP_FIRST_NAME").trim());
                v.add(rs.getString("EMP_LAST_NAME").trim());
                v.add(rs.getString("EMP_CONDITION").trim());

            }
            rs.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

    }

    private void loadDate() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
        lbDate.setText(dateFormat.format(date));
    }

    private void loadEmpName() {
        String firstName = v.get(2);
        String lastName = v.get(3);
        lbEmpName.setText(lastName + " " + firstName);
    }

    private void loadEmpClass() {
        if (v.get(4).equals("01")) {
            lbEmpClass.setText("Inside");
        } else {
            lbEmpClass.setText("Outside");
        }
    }

    private void loadWorkClass() {
        cbWorkingClass.removeAllItems();
        cbWorkingClass.addItem("Normal");
        cbWorkingClass.addItem("Absent");
        cbWorkingClass.addItem("Holiday");

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        String dateSelect = dateFormat.format(date);
        int daySelect = cal.get(Calendar.DAY_OF_WEEK);
        boolean isIn = false;

        ResultSet rs = null;
        String query = "";
        query += "SELECT ";
        query += "* ";
        query += "FROM ";
        query += "CALENDAR_MST ";
        try {
            rs = conn3.excuteQuery(query);
            while (rs.next()) {
                if (dateSelect.equals(rs.getString("CAL_YMD"))) {
                    isIn = true;
                }
            }

            if ((isIn && (daySelect != 1 || daySelect != 7)) || (!isIn && (daySelect == 1 || daySelect == 7))) {
                cbWorkingClass.setSelectedItem("Holiday");
            } else {
                cbWorkingClass.setSelectedItem("Normal");

            }

        } catch (SQLException ex) {
            Logger.getLogger(frm_STAFF_A03.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private void excuteQueryToUpdate() {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String daySelect = dateFormat.format(date); // Ngày lấy từ A04

        ResultSet rs = null;
        String query = "SELECT ";
        query += "* ";
        query += "FROM ";
        query += "WORKTIME WRK ";
        query += "WHERE ";
        query += "WRK.EMP_CODE = '" + EmpCode + "' ";
        query += "AND ";
        query += "WRK.CAL_YMD = '" + daySelect + "' ";
        try {
            rs = conn3.excuteQuery(query);
            while (rs.next()) {
                vectorUpdate.add(rs.getString("WRK_KBN").trim());
                vectorUpdate.add(rs.getString("WRK_GO_KBN").trim());
                vectorUpdate.add(rs.getString("WRK_LEFT_KBN").trim());
                vectorUpdate.add(rs.getString("WRK_START_TIME").trim());
                vectorUpdate.add(rs.getString("WRK_END_TIME").trim());
                vectorUpdate.add(rs.getString("WRK_NOTE").trim());
                vectorUpdate.add(rs.getString("WRK_FLAG_CHECK").trim());
            }

        } catch (SQLException ex) {
            Logger.getLogger(frm_STAFF_A03.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void loadWorkClassUpdate() {
        String workClass = getCodeName.getWorkClass(vectorUpdate.get(0));
        cbWorkingClass.removeAllItems();
        cbWorkingClass.addItem("Normal");
        cbWorkingClass.addItem("Absent");
        cbWorkingClass.addItem("Holiday");


        for (int i = 0; i < cbWorkingClass.getItemCount(); i++) {
            if (cbWorkingClass.getItemAt(i).equals(workClass)) {
                cbWorkingClass.setSelectedIndex(i);
                break;
            }
        }
    }

    private void loadStartClassUpdate() {
        cbStartClass.removeAllItems();
        cbStartClass.addItem("No Setting");
        cbStartClass.addItem("Normal");
        cbStartClass.addItem("Late");
        cbStartClass.addItem("Morning Absent");
        cbStartClass.addItem("Direct");

        String startClass = getCodeName.getStartClass(vectorUpdate.get(1));
        for (int i = 0; i < cbStartClass.getItemCount(); i++) {
            if (cbStartClass.getItemAt(i).equals(startClass)) {
                cbStartClass.setSelectedIndex(i);
            }
        }

    }

    private void loadEndClassUpdate() {
        cbEndClass.removeAllItems();
        cbEndClass.addItem("No Setting");
        cbEndClass.addItem("Normal");
        cbEndClass.addItem("Early");
        cbEndClass.addItem("Afternoon Absent");
        cbEndClass.addItem("Direct");

        String endClass = getCodeName.getEndClass(vectorUpdate.get(2));

        if (!vectorUpdate.get(2).equals("00")) {
            cbEndClass.setSelectedItem(endClass);
        } else {
            if (vectorUpdate.get(1).equals("00")) {
                cbEndClass.setSelectedItem("No Setting");
            } else {
                Calendar cal = Calendar.getInstance();
                cal.getTime();
                int hour = cal.get(Calendar.HOUR);
                int minute = cal.get(Calendar.MINUTE);

                if (hour >= 17) {
                    if (minute >= 15) {
                        cbEndClass.setSelectedItem("Normal");
                    } else {
                        cbEndClass.setSelectedItem("Early");
                    }
                } else {
                    cbEndClass.setSelectedItem("Early");
                }





            }
        }
    }

    private void loadStartTimeUpdate() {
        String startTimehh = vectorUpdate.get(3).trim().substring(0, 2);
        String startTimemm = vectorUpdate.get(3).trim().substring(2, 4);
        cbStartTimeh.removeAllItems();
        cbStartTimem.removeAllItems();

        // StartTime : Hour
        cbStartTimeh.addItem(" ");
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                cbStartTimeh.addItem("0" + i);
            } else {
                cbStartTimeh.addItem(i);
            }
        }
        // Trị init theo Start Time hour
        for (int i = 0; i < cbStartTimeh.getItemCount(); i++) {
            if (cbStartTimeh.getItemAt(i).toString().trim().equals(startTimehh)) {
                cbStartTimeh.setSelectedIndex(i);
                break;
            }
        }

        // StartTime : Minute
        cbStartTimem.addItem(" ");
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                cbStartTimem.addItem("0" + i);
            } else {
                cbStartTimem.addItem(i);
            }
        }
        // Trị init theo Start Time hour
        for (int i = 0; i < cbStartTimem.getItemCount(); i++) {
            if (cbStartTimem.getItemAt(i).toString().trim().equals(startTimemm)) {
                cbStartTimem.setSelectedIndex(i);
                break;
            }
        }

    }

    private void loadEndTimeUpdate() {
        // EndTime : Hour
        cbEndTimeh.removeAllItems();
        cbEndTimem.removeAllItems();
        cbEndTimeh.addItem(" ");
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                cbEndTimeh.addItem("0" + i);
            } else {
                cbEndTimeh.addItem(i);
            }
        }
        // EndTime : Minute
        cbEndTimem.addItem(" ");
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                cbEndTimem.addItem("0" + i);
            } else {
                cbEndTimem.addItem(i);
            }
        }

        if (!vectorUpdate.get(2).equals("00")) {
            String endTimehh = vectorUpdate.get(4).trim().substring(0, 2);
            String endTimemm = vectorUpdate.get(4).trim().substring(2, 4);

            for (int i = 0; i < cbEndTimeh.getItemCount(); i++) {
                if (cbEndTimeh.getItemAt(i).toString().trim().equals(endTimehh)) {
                    cbEndTimeh.setSelectedIndex(i);
                }
            }
            for (int i = 0; i < cbEndTimem.getItemCount(); i++) {
                if (cbEndTimem.getItemAt(i).toString().trim().equals(endTimemm)) {
                    cbEndTimem.setSelectedIndex(i);
                }
            }
        } else {
            if (vectorUpdate.get(1).equals("00")) {
                cbEndTimeh.setSelectedIndex(0);
                cbEndTimem.setSelectedItem(0);
            } else {
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                int hh = cal.get(Calendar.HOUR);
                int mm = cal.get(Calendar.MINUTE);

                for (int i = 0; i < cbEndTimeh.getItemCount(); i++) {
                    if (hh < 10) {
                        if (cbEndTimeh.getItemAt(i).toString().endsWith("0" + hh)) {
                            cbEndTimeh.setSelectedIndex(i);
                        }
                    }

                    if (hh >= 10) {
                        if (cbEndTimeh.getItemAt(i).toString().endsWith("" + hh)) {
                            cbEndTimeh.setSelectedIndex(i);
                        }
                    }
                }

                for (int i = 0; i < cbEndTimem.getItemCount(); i++) {
                    if (mm < 10) {
                        if (cbEndTimem.getItemAt(i).toString().equals("0" + mm)) {
                            cbEndTimem.setSelectedIndex(i);
                        }
                    }
                    if (mm >= 10) {
                        if (cbEndTimem.getItemAt(i).toString().equals("" + mm)) {
                            cbEndTimem.setSelectedIndex(i);
                        }
                    }
                }
            }
        }



//        String endTimehh = vectorUpdate.get(4).trim().substring(0, 2);
//        String endTimemm = vectorUpdate.get(4).trim().substring(2, 4);
//        cbStartTimeh.removeAllItems();
//        cbStartTimem.removeAllItems();
//
//        // StartTime : Hour
//        cbStartTimeh.addItem(" ");
//        for (int i = 0; i < 24; i++) {
//            if (i < 10) {
//                cbStartTimeh.addItem("0" + i);
//            } else {
//                cbStartTimeh.addItem(i);
//            }
//        }
//        // Trị init theo Start Time hour
//        for (int i = 0; i < cbStartTimeh.getItemCount(); i++) {
//            if (cbStartTimeh.getItemAt(i).toString().trim().equals(startTimehh)) {
//                cbStartTimeh.setSelectedIndex(i);
//                break;
//            }
//        }
//
//        // StartTime : Minute
//        cbStartTimem.addItem(" ");
//        for (int i = 0; i < 60; i++) {
//            if (i < 10) {
//                cbStartTimem.addItem("0" + i);
//            } else {
//                cbStartTimem.addItem(i);
//            }
//        }
//        // Trị init theo Start Time hour
//        for (int i = 0; i < cbStartTimem.getItemCount(); i++) {
//            if (cbStartTimem.getItemAt(i).toString().trim().equals(startTimemm)) {
//                cbStartTimem.setSelectedIndex(i);
//                break;
//            }
//        }
    }

    private void loadNoteUpdate() {
        String note = vectorUpdate.get(5);

        taNote.setText(note);
    }
}
